from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

from account.models import User
from libs.db import BaseModel
from libs.enums import LogType, LogModule


class Log(BaseModel):
    user = models.ForeignKey(User, verbose_name='操作人', related_name='logs', on_delete=models.CASCADE, null=True, blank=True)
    module = models.CharField('功能模块', max_length=30, choices=LogModule.choices(), default=LogModule.USER.value)
    type = models.CharField('操作类型', max_length=30, choices=LogType.choices(), default=LogType.CREATE.value)
    content = models.CharField('操作内容', max_length=1000, null=True, blank=True)
    content_type = models.ForeignKey(ContentType, related_name='logs', on_delete=models.CASCADE, blank=True, null=True)
    object_id = models.PositiveIntegerField(blank=True, null=True)
    content_object = GenericForeignKey('content_type', 'object_id')

    class Meta:
        verbose_name = 'Log'
        verbose_name_plural = 'Log'
        ordering = ('-id',)
